package com.rui.forkjion;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class ForkJionDemoTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        test1();
//        test2();
        test3();
    }

    /**
     * 普通程序员
     */
    public static void test1(){
        long sum = 0L;
        long start = System.currentTimeMillis();
        for (long i = 1L ; i <= 1000000000 ; i++){
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("sum = " + sum + " 时间：" + (end - start) + "毫秒");
    }

    /**
     * 会使用ForkJoin
     */
    public static void test2() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> task = new ForkJionDemo(0L, 1000000000L);

        //提交任务
        ForkJoinTask<Long> submit = forkJoinPool.submit(task);
        long sum = submit.get();

        long end = System.currentTimeMillis();
        System.out.println("sum = " + sum + " 时间：" + (end - start) + "毫秒");
    }

    //Stream 并行流
    public static void test3(){
        long start = System.currentTimeMillis();
        long sum = LongStream.rangeClosed(0L, 1000000000L).parallel().reduce(0, Long::sum);
        long end = System.currentTimeMillis();
        System.out.println("sum = " + sum + " 时间：" + (end - start) + "毫秒");
    }
}
